/*
	报表table组件
*/
define(
	[
		'require',
		'broada/base/text!./template.hbs',
		'apm/moment',
		'apm/daterangepicker'
	],
	function (require) {
		"use strict";

		var moment = require('apm/moment');
		var daterangepicker = require('apm/daterangepicker');
		var forEach = Ember.EnumerableUtils.forEach;
		var tpl = require('broada/base/text!./template.hbs');
		
		var ExportFormView= Ember.Component.extend({
			layout: Ember.HTMLBars.compile(tpl),
			time: function(timeDifference,param){
				
				var startDate = new Date(this.get("defaultDate") - parseInt(timeDifference));
				var endDate = new Date(this.get("defaultDate"));
				if(param == 'd'){
					startDate = new Date(this.get("startDate"));
					endDate = new Date(this.get("endDate"));
				}else if(param == 'm'){
					startDate = new Date(this.get("startDate"));
					endDate = new Date(this.get("endDate"));
				}
			
				var startDate = APM.util.formatDate(startDate, "MM/dd/yyyy HH:mm");
				var endDate = APM.util.formatDate(endDate, "MM/dd/yyyy HH:mm");
				return {"startDate":startDate,"endDate":endDate};
			},

			dateChange: Ember.observer("startDate","endDate",function(){
					if(!this.get("startDate"))return;
					if(!this.get("endDate"))return;
					this.timeDifference();
			}),

			timeDifference: function(){
				var startDate = +this.get("startDate"),
					endDate = +this.get("endDate"),
					view = this,
					timeDifference = endDate-startDate;
					
				if(timeDifference == 60000){
					view.set("difference","60000");
				}else if(timeDifference == 1800000){
					view.set("difference","1800000");
				}else if(timeDifference == 3600000){
					view.set("difference","3600000");
				}else if(timeDifference == 86400000 && new Date(this.get("endDate")).getMinutes() == 0 && new Date(this.get("endDate")).getHours() == 0){
					view.set("difference","86400000d");
				}else if(timeDifference == 604800000 && new Date(this.get("endDate")).getMinutes() == 0 && new Date(this.get("endDate")).getHours() == 0){
					view.set("difference","604800000");
				}else if(timeDifference == 604800000){
					view.set("difference","604800000");
				}else if(timeDifference == 2678400000 && new Date(this.get("endDate")).getMinutes() == 0 && new Date(this.get("endDate")).getHours() == 0){
					view.set("difference","2678400000m");
				}else if(timeDifference == 2678400000){
					view.set("difference","2678400000");
				}else if(timeDifference == 86400000){
					view.set("difference","86400000");
				}

			},

			difference: "60000",

			defaultDateChange: Ember.observer("defaultDate",function(){


				if(!this.get("defaultDate")){
						return;
				}

				var ctrl = this.get("controller"),
					timeDifference = this.get("difference"),
				    time,
				    clickTimeStart,
				    clickTimeEnd;

				if(timeDifference.indexOf("d") != -1){
					timeDifference =  timeDifference.substring(0,timeDifference.indexOf("d")+1);
					clickTimeStart = this.get("startDate");
				    clickTimeEnd =  this.get("endDate");
					time = this.time(timeDifference,"d");
				}else if(timeDifference.indexOf("m") != -1){
				    timeDifference = timeDifference.substring(0,timeDifference.indexOf("m")+1);
				    clickTimeStart = this.get("startDate");
				    clickTimeEnd =  this.get("endDate");
					time = this.time(timeDifference,"m");
				}else{
					time = this.time(timeDifference);
					clickTimeStart =  new Date(this.get("defaultDate")+60000 - parseInt(timeDifference));
					clickTimeEnd = new Date(this.get("defaultDate")+60000);
				}

				
				var $viewRoot = this.$();

				$viewRoot.find('#config-demo').val(time.startDate+' - '+time.endDate);


				ctrl.sendAction("clickTimePoint",{startDate:clickTimeStart,endDate:clickTimeEnd});

			}),
		   
			didInsertElement: function(){
			 	var ctrl = this.get("controller");
			 	var view = this;
			 	 if(view.get("position") == 'right'){
			 	 	$(".calendar-box i").css({"right":"240px","left":"12px"})
			 	 	$(".calendar-box input").css({"width":"225px","paddingLeft":"32px"})
			 	 }


			 	
			  	var $viewRoot = this.$();
			  	//当前一天
			  	var sDate = new Date(new Date() - timeD_value),eDate = new Date(new Date() - timeD_value);
			  		sDate.setHours(0);
					sDate.setMinutes(0);
					sDate.setSeconds(0);
					eDate.setDate(sDate.getDate()+1);
					eDate.setHours(0);
					eDate.setMinutes(0);
					eDate.setSeconds(0);

					//当前一周
			  	var WsDate = new Date(new Date() - timeD_value),WeDate = new Date(new Date() - timeD_value);
			  		WsDate.setHours(0);
					WsDate.setMinutes(0);
					WsDate.setSeconds(0);
					var num = WsDate.getDay();
					WsDate.setDate(WsDate.getDate()-num+1);

					WeDate.setDate(WsDate.getDate()+7);
					WeDate.setHours(0);
					WeDate.setMinutes(0);
					WeDate.setSeconds(0);

		
				//当前一个月
				var MsDate = new Date(new Date() - timeD_value),MeDate = new Date(new Date() - timeD_value);
				MsDate.setDate(1);
				MsDate.setHours(0);
				MsDate.setMinutes(0);
				MsDate.setSeconds(0);
				var MeDate = new Date(+MsDate);
				MeDate.setMonth(MeDate.getMonth()+1);

				//全部时间
			  	var currentTime = APM.util.formatDate(new Date(new Date() - timeD_value-60000), "yyyy/MM/dd H:mm");
			  	var oneMinuteBefore = APM.util.formatDate(new Date(new Date() - timeD_value - 60000-60000), "yyyy/MM/dd H:mm");
			  	var MinuteBefore = APM.util.formatDate(new Date(new Date() - timeD_value - 60000-60000*30), "yyyy/MM/dd H:mm");
			  	var oneHourBefore = APM.util.formatDate(new Date(new Date() - timeD_value - 3600000-60000), "yyyy/MM/dd H:mm");
			  	var theDayBefore = APM.util.formatDate(new Date(new Date() - timeD_value - 3600000*24-60000), "yyyy/MM/dd H:mm");
			  	var thePreviousWeek = APM.util.formatDate(new Date(new Date() - timeD_value - 3600000*24*7-60000), "yyyy/MM/dd H:mm");
			  	var oneMonthAgo = APM.util.formatDate(new Date((new Date().setMonth(new Date().getMonth() - 1)) - timeD_value-60000), "yyyy/MM/dd H:mm");
			  	var currentStartDay = APM.util.formatDate(new Date(sDate), "yyyy/MM/dd H:mm");
			  	var currentEndDay = APM.util.formatDate(new Date(eDate), "yyyy/MM/dd H:mm");
			  	var currentStartWeek = APM.util.formatDate(new Date(WsDate), "yyyy/MM/dd H:mm");
			  	var currentEndWeek = APM.util.formatDate(new Date(WeDate), "yyyy/MM/dd H:mm");
			  	var currentStartMonth = APM.util.formatDate(new Date(MsDate), "yyyy/MM/dd H:mm");
			  	var currentEndMonth = APM.util.formatDate(new Date(MeDate), "yyyy/MM/dd H:mm");

			  	var allDate = {
			        "1分钟": [
			           oneMinuteBefore,
			           currentTime   
			        ],
			       
			        "1小时": [
			           oneHourBefore,
			           currentTime   
			        ],
			       
			        "1天": [
			           theDayBefore,
			            currentTime
			        ],
			        "7天": [
			            thePreviousWeek,
			            currentTime
			        ],
			        "1月": [
			            oneMonthAgo,
			            currentTime
			        ],
			        "当天": [
			            currentStartDay,
			            currentEndDay
			        ],
			        "当周": [
			            currentStartWeek,
			            currentEndWeek
			        ],
			        "当月": [
			            currentStartMonth,
			            currentEndMonth
			        ]
			    };

			    var  partDate = {
			        "当天": [
			            currentStartDay,
			            currentEndDay
			        ],
			        "当周": [
			            currentStartWeek,
			            currentEndWeek
			        ],
			        "当月": [
			            currentStartMonth,
			            currentEndMonth
			        ]
			    };

			  	$('#config-demo').daterangepicker({
				    "timePicker": true,
				    "timePicker24Hour": true,
				    // "ranges": view.get("whenDate")?partDate:allDate,
				    "ranges": allDate,
				    "locale": {
				        "direction": "ltr",
				        "format": "YYYY/MM/DD HH:mm",
				        "separator": " - ",
				        "applyLabel": "确定",
				        "cancelLabel": "取消",
				        "fromLabel": "From",
				        "toLabel": "To",
				        "customRangeLabel": "自定义",
				        "daysOfWeek": [
				            "日",
				            "一",
				            "二",
				            "三",
				            "四",
				            "五",
				            "六"
				        ],
				        "monthNames": [
				            "1月",
				            "2月",
				            "3月",
				            "4月",
				            "5月",
				            "6月",
				            "7月",
				            "8月",
				            "9月",
				            "10月",
				            "11月",
				            "12月"
				        ],
				        "firstDay": 1
				    },
				    "startDate": view.get("whenDate") == 7?thePreviousWeek:(view.get("whenDate")?currentStartDay:oneMinuteBefore),     
				    "endDate": view.get("whenDate") == 7?currentTime:(view.get("whenDate")?currentEndDay:currentTime), 
				    "opens": view.get("position")== "right"?"right":"left",
				}, function(start, end, label) {
					var data = {"startDate":start._d.setSeconds(0),"endDate":end._d.setSeconds(0)};
					ctrl.sendAction("timeRanges",data);
			
				});
				 // $('.calendar-box i').click(function() {
		   //        $(this).parent().find('input').click();
		   //        	if($(".daterangepicker.dropdown-menu").hasClass("show-calendar")){
					// 	$(".daterangepicker.dropdown-menu").css("width",550);
					// }else{
					// 	$(".daterangepicker.dropdown-menu").css("width","auto");
					// };
		   //      });
				 // $('.calendar-box input').click(function() {
		   //        	if($(".daterangepicker.dropdown-menu").hasClass("show-calendar")){
					// 	$(".daterangepicker.dropdown-menu").css("width",550);
					// }else{
					// 	$(".daterangepicker.dropdown-menu").css("width","auto");
					// };
		   //      });
				// $('.ranges li:last').on("click",function(){
				// 	if($(".daterangepicker.dropdown-menu").hasClass("show-calendar")){
				// 		$(".daterangepicker.dropdown-menu").css("width",550);
				// 	}else{
				// 		$(".daterangepicker.dropdown-menu").css("width","auto");
				// 	};
				// });



			}
		});

		return ExportFormView;
	}
);